package com.ys.controller.api.business;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.ys.util.RedisUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.entity.business.TpProductBq;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.business.TpProductBqService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/inter/tpProductBq")
public class ApiInterTpProductBqController {

	public static Long MENU_ID = 10169L;
	private static final Logger logger = LoggerFactory.getLogger(ApiInterTpProductBqController.class);

	@Autowired
	private CommonApiService<TpProductBq> commonApiService;

	@Autowired
	private TpProductBqService tpProductBqService;


	@Autowired
	private RedisUtil redisUtil;



	@RequiresPermissions("tpProductBq:view")
	@RequestMapping("/search")
	@ResponseBody
	public LayuiTableVoJA search(@ModelAttribute TpProductBq cond){
		cond.addLeftJoin("TP_ProductBqSp B on  B.pid = A.id")
		.addLeftJoin("YS_ProcInst C ON  C.Bid = A.Bid")
		.addField("B.code,B.name,B.priceService,B.codeMerge,B.isSuperior,B.vsfJp,B.commodityProperty,C.UpdateTime updateTime1")
		.addLeftJoin("THR_OperateCompanyApply THRO ON  THRO.VGsBm = A.VgsBm")
		.addField("THRO.CUser AS cUser,THRO.VGsMc AS vgsMc,THRO.VGsJc AS vgsJc");
		cond.addLeftJoin("TS_UserChange t2 on  A.CreateChangeId = t2.id");
		cond.addField("CONCAT(t2.UserId,'/', t2.UserName) as createUserInfo");

		//权限
		cond = commonApiService.dataAuth(MENU_ID, cond);
		cond.setOrderby("A.createTime DESC");
		return commonApiService.searchLay(cond);
	}

	/**
	 * 新增
	 */
	@RequiresPermissions("tpProductBq:add")
	@RequestMapping("/toAdd")
	@ResponseBody
	public R toAdd(HttpServletRequest request, @RequestParam Map<String, Object> params,String subData) {
		try {
			logger.info("商品备案申请新增传参：" + params);
			return tpProductBqService.add(params,subData);
		} catch (Exception e) {
			logger.error("商品备案申请新增出错：", e.getMessage(), e);
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}
	}


	/**
	 * 修改
	 */
	@RequiresPermissions("tpProductBq:add")
	@RequestMapping("/toEdit")
	@ResponseBody
	public R toEdit(HttpServletRequest request, @RequestParam Map<String, Object> params,String subData) {
		try {
			logger.info("商品备案申请修改传参：" + params);
			//修改
			return tpProductBqService.update(params,subData);
		} catch (Exception e) {
			logger.error("修改商品备案申请出错：", e.getMessage(), e);
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}
	}

	/**
	 * 删除
	 */
	@RequiresPermissions("tpProductBq:add")
	@RequestMapping("/del")
	@ResponseBody
	public R del(HttpServletRequest request,@RequestParam("list") List<String> idList){
		try {
			tpProductBqService.del(idList);
			return R.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("操作失败，请联系客服");
		}
	}

	/**
	 * 删除商品备案申请
	 */
	@RequiresPermissions("tpProductBq:add")
	@PostMapping("/delete")
	@ResponseBody
	public R delete(Long id){
		try {
			return tpProductBqService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("操作失败，请联系客服");
		}
	}

	/**
	 * 流转通过
	 */
	@RequiresPermissions("tpProductBq:add")
	@PostMapping("/flowPass")
	@ResponseBody
	public R flowPass(HttpServletRequest request, Long id){
		try {
			return tpProductBqService.flowPass(id, ShiroUtils.getUserId());
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("服务器正在开小差，请联系客服");
		}
	}

	/**
	 * 流转驳回
	 */
	@RequiresPermissions("tpProductBq:add")
	@PostMapping("/flowRefuse")
	@ResponseBody
	public R flowRefuse(HttpServletRequest request, Long id, String reason){

		try {
			return tpProductBqService.flowRefuse(id, reason, ShiroUtils.getUserId());
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("服务器正在开小差，请联系客服");
		}
	}



	/**
	 * 流转驳回
	 */
	@RequiresPermissions("tpProductBq:add")
	@PostMapping("/flowInit")
	@ResponseBody
	public R flowInit(HttpServletRequest request, Long id, String reason){

		try {
			return tpProductBqService.flowInit(id, reason, ShiroUtils.getUserId());
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("服务器正在开小差，请联系客服");
		}
	}

	@RequestMapping("/export")
	@ResponseBody
	public R export(@ModelAttribute TpProductBq cond, String conditions) throws Exception{

		cond.setPage(0).setSize(999999);
		cond.addLeftJoin("TP_ProductBqSp B on  B.pid = A.id")
		.addLeftJoin("YS_ProcInst C ON  C.Bid = A.Bid")
		.addField("B.code,B.name,B.priceService,B.codeMerge,B.isSuperior,B.vsfJp,B.commodityProperty,C.UpdateTime updateTime1,'已生效' effectStock1")
		.addLeftJoin("THR_OperateCompanyApply THRO ON  THRO.VGsBm = A.VgsBm")
		.addField("THRO.VGsMc AS vgsMc,THRO.VGsJc AS vgsJc");
		cond.addLeftJoin("TS_UserChange t2 on  A.CreateChangeId = t2.id");
		cond.addField("CONCAT(t2.UserId,'/', t2.UserName) as createUserInfo");

		cond.setOrderby("A.createTime DESC");

		List<Map<String, Object>> listData = cond.queryMap();


		List<YsCol> listField = new YsCol()
				.setMenuId(MENU_ID)
				.setFrontShow(YsColEnum.FrontShow.SHOW.value)
				.setOrderby("A.SortNo asc")
				.queryList();

		ExportExcelUtils<TpProductBq> util = new ExportExcelUtils<TpProductBq>(new TpProductBq());

		String toFilePath = util.exportExcel(listData, listField, "商品备案申请表");
		UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
		return R.ok(dto.getUrl());
	}


	/**
	 * 优品导入后，删除redis中的单号缓存信息
	 */
	@PostMapping("/importYpDelRedis")
	@ResponseBody
	public R importYpDelRedis(HttpServletRequest request){
		try {
			String loginUserId = ShiroUtils.getUserId();
			redisUtil.hPut("importInfoYp",loginUserId,"");
			return R.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("服务器正在开小差，请联系客服");
		}
	}

}